import axios from '@/axios'
import local_vuex from "@task/js/vuex"
import local_script from "@task/js/util"
import global_script from '@script/util'
import encrypt from '@script/encrypt'

export default {
    /**
     * 发送 Axios 请求，创建任务
     * @param {String} subject 任务主题
     * @param {String} describe 任务描述
     * @param {Number} issuer 发布者
     * @param {Date} deadLine 截止日期
     * @param {Array} imgList 图片清单
     */
    issue_task(subject, describe, issuer, deadLine, imgList) {
        axios({
            url: `/task/create/record`,
            params: local_script.getIssueParams(subject, describe, issuer, deadLine, imgList),
            method: "POST"
        })
    },
    /**
     * 发送 Axios 请求，查询指定参数的任务信息，并根据情况，删除已截止的任务
     * @param {Number} sid 待查询对象的学号
     * @param {Number} issuer 任务发布者
     * @param {String} dept 部门名称
     */
    async queryTask(sid, issuer, dept, close = local_vuex.state.myTask.close, complete = local_vuex.state.myTask.complete) {
        let res = await axios({
            url: `/task/get/task`,
            method: "GET",
            params: local_script.getQueryParams(sid, issuer, dept, close, complete)
        })
        if(local_vuex.state.myTask.close === 0) {
            res = local_script.rmClosedTask(res)
        }
        return res
    },
    /**
     * 发送 Axios 请求，提交任务（文字内容、图片清单）
     * @param {Object} task 任务对象
     * @param {Number} sid 提交人的学号
     */
    commit(task, sid) {
        let imgs = task.imgs.length === 0 ? undefined : task.imgs
        axios({
            url: `/task/post/task`,
            params: local_script.getCommitContent(task, sid, imgs),
            method: "POST"
        })
    },
    /**
     * 发送 Axios 请求，将指定 id 的任务置为已截止
     * @param {String} uid 任务 id
     */
    closeTask(uid) {
        axios({
            url: `/task/alter/close`,
            params: {
                tid: uid
            },
            method: "PATCH"
        })
    },
    /**
     * 查询指定 sid 学生，从起始日期到截止日期之间的任务情况
     * @param {Number} sid 待查询学生的学号
     * @param {Date} startDay 起始日期
     * @param {Data} endDay 截止日期
     */
    async taskStatistics(sid, startDay, endDay) {
        return await axios({
            method: "GET",
            url: `/task/get/statistics`,
            params: {
                sid: encrypt(sid),
                start: startDay,
                end: endDay
            }
        })
    },
    async getCommited(tid, department, group) {
        return await axios({
            url: "/task/get/commited",
            params: {
                tid: tid,
                department: department,
                group: group
            },
            method: "GET"
        })
    }
}